package com.kun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kun.entity.Blog;
import com.kun.mapper.BlogMapper;
import com.kun.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author SunKun
 * @since 2021-06-14
 */
@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogMapper blogMapper;

    /**
     * 查询博客发布时间和数量
     * @return
     * @throws Exception
     */
    @Override
    public List<Blog> queryReleaseDateAndCount() throws Exception {
        return blogMapper.queryReleaseDateAndCount();
    }

    /**
     * 查询博客类型或博客发布日期(index主要是区别两者的 1、类型 2、日期 3、Java )
     * @param page
     * @param blog
     * @param index
     * @return
     */
    @Override
    public IPage<Blog> queryBlogByPage(IPage<Blog> page, Blog blog,int index) {
        switch (index){
            case 1 : return blogMapper.queryBlogTypeIdByPage(page,blog);
            case 2 : return blogMapper.queryBlogDateByPage(page,blog);
            default : return blogMapper.queryBlogByTypeName(page,blog.getTypeId());
        }
    }

    /**
     * 分页查询
     * @param current
     * @param size
     * @return
     */
    @Override
    public Page<Blog> queryPage(int current, int size) {
        Page<Blog> page = new Page<>(current,size);
        QueryWrapper<Blog> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("releaseDate");
        blogMapper.selectPage(page, wrapper);
        return page;
    }

    /**
     * 查看博客详情
     * @param id
     * @return
     */
    @Override
    public Blog queryBlogById(int id) throws Exception{
        //先查询博客信息
        Blog blog = blogMapper.queryBlogById(id);
        //再修改阅读数量
        blog.setClickHit(blog.getClickHit()+1);
        //调用修改方法
        blogMapper.updateById(blog);
        //返回博客信息
        return blog;
    }

    /**
     * 查询当前博客的上一篇和下一篇博客
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public List<Blog> queryBlogByPrevAndNext(int id) throws Exception {
        return blogMapper.queryBlogByPrevAndNext(id);
    }

    /**
     * 添加博客
     * @param blog
     * @return
     */
    @Override
    public int insertBlog(Blog blog) throws Exception{
        return blogMapper.insert(blog);
    }

    /**
     * 后台博客查询分页
     * @param page
     * @return
     */
    @Override
    public IPage<Blog> queryPage(IPage<Blog> page) {
        return blogMapper.queryPage(page);
    }

    /**
     * 删除博客
     * @param id
     * @return
     */
    @Override
    public int delBlog(int id) {
        return blogMapper.deleteById(id);
    }

    @Override
    public Blog selectBlogById(int id) throws Exception {
        return blogMapper.queryBlogById(id);
    }

    /**
     * 更改博客
     * @param blog
     * @return
     */
    @Override
    public int updateBlogById(Blog blog) { ;
        return blogMapper.updateById(blog);
    }

    /**
     * 批量更新
     * @param blogList
     * @return
     */
    @Override
    public int batchUpdateBlog(List<Blog> blogList) {
        return blogMapper.batchUpdateBlog(blogList);
    }

    /**
     * 批量删除
     * @param blogList
     * @return
     */
    @Override
    public int batchDeleteBlog(List<Blog> blogList) {
        return blogMapper.batchDeleteBlog(blogList);
    }
}
